<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-12
 * Time: 14:18
 */

namespace App\Http\Controllers\Admin;

use App\Base\BaseFunc;
use App\Base\Extract;
use App\Base\Power;
use App\Http\Controllers\Controller;
use App\Http\Requests\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use App\Base\ImgDB;

class AdminController extends Controller
{
    public function adminLogin()
    {
        return view("/admin.adminLogin");
    }

    public function _adminLogin(BaseFunc $baseFunc)
    {
        $input = Input::only('admin_name','admin_password');
        $name = 'admin';
        $password = 'admin';
        if ($input['admin_name'] == $name && $input['admin_password'] == $password)
        {
            session(['admin_id' => 1]);
            session(['admin_name' => 'admin']);
            $baseFunc->setRedirectMessage(true,'登陆成功！',NULL,'/adminHome');
        }
        else
        {
            $baseFunc->setRedirectMessage(false,'登陆错误，请重新登陆！',NULL,'/adminLogin');
        }
    }

    public function adminLogout(BaseFunc $baseFunc)
    {
        session(["admin_id"=>NULL]);
        $baseFunc ->setRedirectMessage(true,"您已退出登录",NULL,"/adminLogin");
    }

    public function adminHome()
    {
        $data['adData'] = DB::table('ad')
            ->orderby('ad_id','desc')
            ->paginate(10);
        session(['now_address'=>'/adminHome']);
        return view('/admin.adminHome',$data);
    }

    public function addAD(BaseFunc $baseFunc,Extract $extract)
    {
        $input = Input::only('ad_name','ad_info','ad_floor');
        $input['ad_time'] = date('Y-m-d H:i:s');
        $input['ad_up'] = 0;
        DB::table('ad')->insert($input);
        $imgs = $extract->extract_attrib($input['ad_info']);
        $from = DB::table('ad')->orderby('ad_id','desc')->first();
        foreach ($imgs as $img)
        {
            foreach ($img as $item)
            {
                $simgs = substr($item,21,23);
                DB::table('images')->insert([
                    'images_name' => $simgs,
                    'images_from_ad' => $from->ad_id
                ]);
            }
        }
        $baseFunc->setRedirectMessage(true,"添加公告成功！",NULL,"/adminHome");
    }

    public function adInfo($ad_id)
    {
        $data['adInfo'] = DB::table('ad')
            ->where('ad_id',$ad_id)
            ->first();
        return view('/admin.adInfo',$data);
    }

    public function delAd(BaseFunc $baseFunc)
    {
        $input = Input::only('ad_id');
        $data = DB::table('ad')
            ->where('ad_id','=',$input['ad_id'])
            ->delete();
        if($data)
        {
            $baseFunc->setRedirectMessage(true,'删除成功',NULL,'/adminHome');
        }
        else
        {
            $baseFunc->setRedirectMessage(false,'删除失败',NULL,'/adminHome');
        }
    }

    public function upToLogin($id)
    {
        DB::table('ad')->where('ad_id',$id)
            ->update([
               'ad_login' => 1
            ]);
        return redirect()->back();
    }

    public function downToLogin($id)
    {
        DB::table('ad')->where('ad_id',$id)
            ->update([
                'ad_login' => 0
            ]);
        return redirect()->back();
    }

    public function searchMPAction()
    {
        $data['mpActionData'] = DB::table('mp_action')
            ->orderby('mp_action_id','desc')
            ->paginate(10);
        session(['now_address'=>'/searchMPAction']);
        return view('/admin.hu.searchMPAction',$data);
    }

    public function addMPAction(BaseFunc $baseFunc)
    {
        $input = Input::only('mp_action_name','mp_action_info','mp_action_floor');
        $input['mp_action_time'] = date('Y-m-d H:i:s');
        DB::table('mp_action')->insert($input);
        $baseFunc->setRedirectMessage(true,"添加门派活动成功！",NULL,"/searchMPAction");
    }

    public function mpActionInfo($id)
    {
        $data['mpActionInfo'] = DB::table('mp_action')
            ->where('mp_action_id',$id)
            ->first();
        return view('/admin.hu.mpActionInfo',$data);
    }

    public function delMPAction(BaseFunc $baseFunc)
    {
        $input = Input::only('mp_action_id');
        $data = DB::table('mp_action')
            ->where('mp_action_id','=',$input['mp_action_id'])
            ->delete();
        if($data)
        {
            $baseFunc->setRedirectMessage(true,'删除成功',NULL,'/searchMPAction');
        }
        else
        {
            $baseFunc->setRedirectMessage(false,'删除失败',NULL,'/searchMPAction');
        }
    }
}